package com.demo.example.hot100;

public class TheMaxSumOfTheArray {
    public static void main(String[] args) {
        int[] nums=new int[]{
                -2,1,-3,4,-1,2,1,-5,4
        };
        int[] nums2=new int[]{
                5,4,-1,7,8
        };
        //System.out.println(maxSubArray(nums));
        System.out.println(maxSubArray(nums2));
    }
    public static int maxSubArray(int[] nums){
        int max=Integer.MIN_VALUE;
        if(nums.length==1){
            return nums[0];
        }
        int tmp=nums[0];
        for(int i=1;i<nums.length;i++){
            max=tmp>max?tmp:max;
            if(tmp<0){
                tmp=nums[i];
            }else{
                tmp+=nums[i];
            }
        }
        return tmp>max?tmp:max;
    }
}
